﻿using System;
using System.Collections.Generic;
using System.Linq;
using Abreu.Base.ViewModels.Interfaces;
using System.Collections.ObjectModel;
using System.Windows.Controls;
//using Abreu.Base.Controls;

namespace Abreu.Base.ViewModels.Managers
{
    public class TabManagerVM : IManager
    {

        public ObservableCollection<TabContainerItem> ContainerTabs { get; set; }

        public List<IViewModel> ViewModels { get; set; }

        public IViewModel CurrentViewModel
        {
            get
            {
                TabContainerItem currentTab = ContainerTabs.Single(t => t.IsSelected);

                _CurrentViewModel = ((UserControl)currentTab.Content).DataContext as IViewModel;

                return _CurrentViewModel;
            }
            set
            {
                _CurrentViewModel = value;
            }
        }
        private IViewModel _CurrentViewModel;

        public TabManagerVM()
        {
            this.ContainerTabs = new ObservableCollection<TabContainerItem>();
            this.ViewModels = new List<IViewModel>();
        }

        public TabManagerVM(IViewModel parent)
        {
            this.ContainerTabs = new ObservableCollection<TabContainerItem>();
            this.ViewModels = new List<IViewModel>();
            this.Parent = parent;
        }

        public void AddTab(string name, UserControl tabContent, IViewModel viewModel)
        {
            TabContainerItem ti = new TabContainerItem(name, tabContent, viewModel);
            ti.CloseClicked += new CloseButtonClick(ti_CloseClicked);
            this.ContainerTabs.Add(ti);
        }

        void ti_CloseClicked(object sender, EventArgs e)
        {
            Grid g = ((Button)sender).Parent as Grid;

            ((MainVM)this.Parent).RemoveTab(g.Parent as TabContainerItem);
        }

        public string Name
        {
            get { throw new NotImplementedException(); }
        }

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        private MainVM mainVM;
        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }

        public IViewModel Parent
        {
            get;
            set;
        }

        internal void RemoveTab(TabContainerItem tabContainerItem)
        {
            this.ContainerTabs.Remove(tabContainerItem);
        }
    }
}
